package com.company.project.common.job.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.job.CorporOV;
import com.company.project.common.redismq.ZSetVer;
import com.company.project.common.utils.StringUtil;
import com.company.project.entity.*;
import com.company.project.mapper.*;
import com.company.project.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component("hwtjFzsTask")
public class HwtjFzsTask {
    @Resource
    public TSeatsMapper tSeatsMapper;
    @Resource
    public TWhbcsMapper tWhbcsMapper;
    @Resource
    public RedisService redisService;
    @Resource
    public TMiddlenumMapper tMiddlenumMapper;
    @Resource
    public TXiaohaoMapper tXiaohaoMapper;
    @Resource
    public THwtjFzsMapper tHwtjFzsMapper;
    @Autowired
    private TThjlmxMapper thjlmxMapper;
    @Autowired
    private TCallbakLogMapper tCallbakLogMapper;
    @Resource
    private ZSetVer zSetVer;
    public final static String RS_ZS_MQ_ME = "consumerDelayMessage:";

    public void run(String params) {

        List<CorporOV> ovs = tWhbcsMapper.selectListCo();
        List<TMiddlenumEntity> middlenumEntityList = tWhbcsMapper.selectListM();
        if (ovs != null && ovs.size() > 0) {
            tWhbcsMapper.deleteListC(ovs);
        }
        if (middlenumEntityList != null  && middlenumEntityList.size() > 0) {
            tWhbcsMapper.deleteListM(middlenumEntityList);
        }

        for (CorporOV ov : ovs) {
            //有相同的公司统计分钟数
            //公司主坐席
            TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                    .eq(TSeatsEntity::getCorporatename, ov.getCorporatename()
                    ).eq(TSeatsEntity::getGsgl, "1"));
            Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getMinutes();
            tSeatsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);

            tHwtjFzsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);
        }
        for (TMiddlenumEntity ov : middlenumEntityList) {
            String middlenum = ov.getMiddlenum();
            int minutes = ov.getSycs();
            //中间号  减去剩余分钟数
            TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                    .eq(TMiddlenumEntity::getMiddlenum, middlenum));
            tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
            tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
            log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
            tMiddlenumMapper.updateById(tMiddlenumEntity);
            tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
        }

        List<TThjlmxEntity> list  = thjlmxMapper.getTwos();

        if(list != null && list.size() > 0) {
            log.info("getTwos:{}", list.toString());
            thjlmxMapper.deleteListById(list);
//            log.info("insertList:{}", list.toString());
            thjlmxMapper.insertList(list);
            log.info("去重成功");
        }

        //You, 2025/04/04 下午 04:04:04 添加

        //查询没有状态有录音的通话记录明细
        List<TThjlmxEntity> listNoStart = thjlmxMapper.selectNoStart();

        //处理好的数据
        List<TThjlmxEntity> listStart = new ArrayList<>();
        List<String> listBindId = new ArrayList<>();

        if (listNoStart != null && listNoStart.size() > 0) {
            List<TCallbakLogEntity> listNoStartLog = tCallbakLogMapper.selectListByNoStart();
            for(TThjlmxEntity tThjlmxEntity : listNoStart)  {
                for(TCallbakLogEntity tCallbakLogEntity : listNoStartLog) {
                    if(tThjlmxEntity.getBindId().equals(tCallbakLogEntity.getBindId())) {
                        tThjlmxEntity.setStart(tCallbakLogEntity.getStatus());
                        tThjlmxEntity.setZjjttime(tCallbakLogEntity.getStartTime());
                        tThjlmxEntity.setBjjttime(tCallbakLogEntity.getCallOutStartTime());
                        tThjlmxEntity.setEndtime(tCallbakLogEntity.getEndTime());
                        tThjlmxEntity.setZjhm(tCallbakLogEntity.getZjhm());
                        tThjlmxEntity.setBjhm(tCallbakLogEntity.getBjhm());
                        tThjlmxEntity.setXh(tCallbakLogEntity.getZjh());
                        tThjlmxEntity.setThsc(tCallbakLogEntity.getDuration());

                        tThjlmxEntity.setStartinfo(tCallbakLogEntity.getUlFailReason());

                        listStart.add(tThjlmxEntity);
                        listBindId.add(tThjlmxEntity.getBindId());
                    }
                }
            }

            if(listBindId.size()>0) {
                //删除没有状态数据
                thjlmxMapper.deleteListById(listStart);
                //插入新数据
                thjlmxMapper.insertList(listStart);
            }
        }

//        thjlmxMapper.deleteEndS();//删除重复的通话记录明
    }

}
